/**
 * 
 */
package com.faxsun.core.order.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.broadleafcommerce.common.web.BroadleafRequestContext;
import org.broadleafcommerce.core.order.dao.OrderItemDao;
import org.broadleafcommerce.core.order.domain.Order;
import org.broadleafcommerce.core.order.domain.OrderAttribute;
import org.broadleafcommerce.core.order.domain.OrderItem;
import org.broadleafcommerce.core.order.service.OrderServiceImpl;
import org.broadleafcommerce.profile.core.domain.Customer;
import org.springframework.transaction.annotation.Transactional;

import com.faxsun.core.order.dao.FSOrderDao;
import com.faxsun.core.order.dao.OrderAttributeDao;
import com.faxsun.core.order.service.type.FSInternalOrderStatus;
import com.faxsun.core.order.service.type.FSOrderStatus;
import com.faxsun.payment.service.gateway.PaymentGatewayConstants;

/**
 * 重写Service中用到OrderStatus的方法
 * 
 * @author zhaoteng.song@faxsun.com
 *
 */
public class FSOrderServiceImpl extends OrderServiceImpl implements FSOrderService {

	private static final Log LOG = LogFactory.getLog(FSOrderServiceImpl.class);

	@Resource(name = "blOrderItemDao")
	protected OrderItemDao orderItemDao;

	@Resource(name = "fsOrderAttributeDao")
	protected OrderAttributeDao orderAttributeDao;

	@Override
	@Transactional("blTransactionManager")
	public Order createNamedOrderForCustomer(String name, Customer customer) {
		Order namedOrder = orderDao.create();
		namedOrder.setCustomer(customer);
		namedOrder.setName(name);
		namedOrder.setStatus(FSOrderStatus.NAMED);

		if (extensionManager != null) {
			extensionManager.getProxy().attachAdditionalDataToNewNamedCart(customer, namedOrder);
		}

		if (BroadleafRequestContext.getBroadleafRequestContext() != null) {
			namedOrder.setLocale(BroadleafRequestContext.getBroadleafRequestContext().getLocale());
		}

		return orderDao.save(namedOrder); // No need to price here
	}

	/**
	 * 数据库中查询orderItem
	 * 
	 * @param itemId
	 * @return
	 */
	public OrderItem readOrderItem(String itemId) {
		Long orderItemId = null;
		OrderItem item = null;
		try {
			orderItemId = Long.parseLong(itemId);
		} catch (NumberFormatException e) {

		}
		if (orderItemId != null) {
			item = orderItemDao.readOrderItemById(orderItemId);
		}
		return item;
	}

	@Override
	public List<Order> retrieveOrderByInternalStatus(FSInternalOrderStatus status) {
		FSOrderDao fsOrderDao = (FSOrderDao) orderDao;

		return fsOrderDao.readOrderByInternalStatus(status);
	}

	@Override
	public List<Order> readOrderByProductId(Long productId) {
		FSOrderDao fsOrderDao = (FSOrderDao) orderDao;
		return fsOrderDao.readOrderByProductId(productId);
	}

	@Override
	@Transactional("blTransactionManager")
	public void deleteOrder(Order order) {
		LOG.warn("Delete order " + order.toString());
		orderMultishipOptionService.deleteAllOrderMultishipOptions(order);
		List<OrderAttribute> attributes = orderAttributeDao.readOrderAttributesForOrder(order);
		LOG.info(attributes);
		for (OrderAttribute orderAttribute : attributes) {
			orderAttributeDao.delete(orderAttribute);
		}
		orderDao.delete(order);
	}

	public String getSellOrderId(Order order) {
		if (order.getOrderAttributes() != null
				&& order.getOrderAttributes().containsKey(PaymentGatewayConstants.PAYMENT_SUBMIT_TIME)) {
			return order.getId().toString() + "_"
					+ order.getOrderAttributes().get(PaymentGatewayConstants.PAYMENT_SUBMIT_TIME).getValue();
		}

		return null;
	}
}
